package com.thb.pay.mchant.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.thb.pay.api.merchant.MchNotifyServiceApi;
import com.thb.pay.api.merchant.vo.req.MchNotifyPageReq;
import com.thb.pay.api.merchant.vo.resp.MchNotifyVo;
import com.thb.pay.core.constant.PayConstant;
import com.thb.pay.core.exception.RRException;
import com.thb.pay.core.utils.PageResult;
import com.thb.pay.core.utils.ThbPayUtil;
import com.thb.pay.db.utils.PageResultUtils;
import com.thb.pay.db.utils.PageUtils;
import com.thb.pay.db.utils.QueryUtils;
import com.thb.pay.mchant.entity.MchNotify;
import com.thb.pay.mchant.service.MchNotifyService;
import com.xiaoleilu.hutool.bean.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 商户-通知表 前端控制器
 * </p>
 *
 * @author lucky
 * @since 2019-07-21
 */
@Slf4j
@RestController
public class MchNotifyController implements MchNotifyServiceApi {

    @Autowired
    private MchNotifyService mchNotifyService;

    @Override
    public PageResult<MchNotifyVo> findPage(MchNotifyPageReq pageReq) {
        IPage<MchNotify> currentPage = PageUtils.buildPage(pageReq);
        QueryWrapper wrapper = QueryUtils.buildQueryPage(pageReq);
        IPage<MchNotify> pageResult = mchNotifyService.page(currentPage, wrapper);
        return PageResultUtils.build(pageResult, MchNotifyVo.class);
    }

    @Override
    public MchNotifyVo findByOrderId(String orderId) {
        MchNotifyVo resultData = null;

        MchNotify dbResult = mchNotifyService.findByOrderId(orderId);
        if (dbResult != null) {
            resultData = new MchNotifyVo();
            BeanUtil.copyProperties(dbResult, resultData);
        }
        return resultData;
    }

    @Override
    public Boolean resend(String orderIds) {
        if(StringUtils.isBlank(orderIds)){
            throw new RRException("请选择重发订单！");
        }
        String[] orderArr = orderIds.split(",");
        if(orderArr.length > 10){
            throw new RRException("批量重发商户通知个数不得大于10个！");
        }

        for(String id: orderArr){
            MchNotify mchNotify = mchNotifyService.findByOrderId(id);
            if(mchNotify.getStatus() == PayConstant.MCH_NOTIFY_STATUS_SUCCESS || StringUtils.isBlank(mchNotify.getNotifyUrl())) continue;

            try {
                byte updateCount = (byte) (mchNotify.getNotifyCount() + 1);
                String result = ThbPayUtil.call4Post(mchNotify.getNotifyUrl());
                if("success".equalsIgnoreCase(result)){
                    mchNotifyService.updateMchNotifySuccess(mchNotify.getOrderId(), result, updateCount);
                }else{
                    mchNotifyService.updateMchNotifyFail(mchNotify.getOrderId(), result, updateCount);
                }
            } catch (Exception e) {
                log.error("重发失败 id={}", id, e);
            }
        }
        return true;
    }
}
